home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / text / hyper / Gguide2txt.lha / Gguide2txt.c < prev    next >
C/C++ Source or Header  |  1995-01-30  |  6KB  |  209 lines

  1. /* Gguide2txt
  2.  *
  3.  * Auto: sc >ERR:<file>.err <path><file>
  4.  *
  5.  * Reads in an Amigaguide file. Strips out all links, controls and linefeeds
  6.  * Paginates the resulting text.
  7.  *
  8.  * Arguments:
  9.  *   First argument is to be the input (guide) path/filename
  10.  *   2nd argument to be the output path/filename.
  11.  *
  12.  * Optional 3rd argument
  13.  *     <textlength> = number of lines of text wanted per page (defaults to 63)
  14.  *                       (Use n0 if no formfeeds wanted)
  15.  *
  16.  * Based on Gstrip by Gnome
  17.  *
  18.  * Author: Gnome  31 Jan 95
  19.  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <exec/types.h>
  24. #include <string.h>
  25. #include <pragmas/dos_pragmas.h>
  26.  
  27. #define AT       '@'
  28. #define QUOTE    '\"'
  29. #define BRA      '{'
  30. #define KET      '}'
  31.  
  32. #define VER    "01.01"
  33.  
  34. const char version[] = "\0$VER: Gguide2txt "VER" ("__DATE__") "__TIME__" by Gnome";
  35.  
  36. extern DOSBase;
  37.  
  38. BOOL DeleteFile(STRPTR);
  39.  
  40. main(argc, argv)
  41. int argc;
  42. char *argv[];
  43.  
  44. {
  45.   FILE *Inhandle, *Outhandle;   /* file pointers  */
  46.   int i, j, linecount, textlength;
  47.   char work[4];
  48.   char infile[80];
  49.   char outfile[80];
  50.   char *result;
  51.   char buffer[600], buff2[600];
  52.   BOOL dunnit, endline, formfeeds, endlink;
  53.  
  54.   linecount=0; textlength=63;
  55.   dunnit=endline=FALSE;
  56.   formfeeds=TRUE;
  57.  
  58.   if (*argv[1] == '?' || argc<3)
  59.   {
  60.     printf("\nGguide2txt Vn%s   by Gnome\n\n",VER);
  61.     printf("Enter the path/inputfile path/outputfile names:\n");
  62.     printf(" Optional 3rd argument <lines-per-page>\n");
  63.     printf("     (63 by default. 0 if formfeeds not wanted.\n\n");
  64.     printf("e.g. %s df0:myguide ram:mytext 66 \n\n",argv[0]);
  65.     return(0);
  66.   }
  67.  
  68.   strcpy(infile,argv[1]);
  69.   strcpy(outfile,argv[2]);
  70.  
  71.   if (argc>3)
  72.   {
  73.     strcpy(work,argv[3]);
  74.     i=atoi(work);
  75.     if (i==0) formfeeds=FALSE;
  76.       else textlength=i;
  77.   }
  78.  
  79.   Inhandle = fopen(infile,"r");
  80.  
  81.   if (Inhandle==0)
  82.   {
  83.     printf("Could not open %s for reading.\n",argv[1]);
  84.     exit (20);
  85.   }
  86.  
  87.   Outhandle = fopen(outfile,"w");
  88.   if (Outhandle==0)
  89.   {
  90.     printf("Could not open %s for writing.\n",outfile);
  91.     fclose(Inhandle);
  92.     exit (20);
  93.   }
  94.  
  95.   printf("\nProcessing %s onto %s\n",infile, outfile);
  96.  
  97. /*------------- main loop ------------------------------------------*/
  98.  
  99.   for (;;)          /* for ever (til break) */
  100.   {
  101.     result = fgets(buffer,599,Inhandle);
  102.     if (result ==0) { dunnit=TRUE; break; }
  103.  
  104.     if (strnicmp(buffer,"@DATABASE",9) ==0) continue;  /* discard these lines */
  105.     if (strnicmp(buffer,"@INDEX",6) ==0) continue;
  106.     if (strnicmp(buffer,"@REMARK",7) ==0) continue;
  107.     if (strnicmp(buffer,"@MASTER",7) ==0) continue;
  108.     if (strnicmp(buffer,"@NODE",5) ==0) continue;
  109.     if (strnicmp(buffer,"@DNODE",6) ==0) continue;
  110.     if (strnicmp(buffer,"@ENDNODE",8) ==0) continue;
  111.     if (strnicmp(buffer,"@TITLE",6) ==0) continue;
  112.     if (strnicmp(buffer,"@PREV",5) ==0) continue;
  113.     if (strnicmp(buffer,"@NEXT",5) ==0) continue;
  114.     if (strnicmp(buffer,"@WIDTH",6) ==0) continue;
  115.     if (strnicmp(buffer,"@TOC",4) ==0) continue;
  116.     if (strnicmp(buffer,"@HEIGHT",7) ==0) continue;
  117.     if (strnicmp(buffer,"@WORDWRAP",9) ==0) continue;
  118.     if (strnicmp(buffer,"@FONT",5) ==0) continue;
  119.     if (strnicmp(buffer,"@HELP",5) ==0) continue;
  120.     if (strnicmp(buffer,"@KEYWORDS",9) ==0) continue;
  121.  
  122.     i=0;
  123.     if (strnicmp(buffer,"@author",7) ==0) i=1;
  124.     if (strnicmp(buffer,"@(c)",4) ==0) i=1;
  125.     if (strnicmp(buffer,"@$VER",5) ==0) i=2;
  126.     if (strnicmp(buffer,"@VER",4) ==0) i=1;
  127.     if(i>0)
  128.     {
  129.       fprintf(Outhandle,"%s",&buffer[i]);
  130.       linecount++;
  131.       continue;
  132.     }
  133.  
  134. /* deal with format commmands & parse into buff2 */
  135.  
  136.     buff2[0]='\0';
  137.     for (i=0;i<strlen(buffer);i++)
  138.     {
  139.       if (buffer[i]=='\0') break;             /* end of line */
  140.       if ((buffer[i]==AT)&(buffer[i+1]==BRA)&(buffer[i+2]!=QUOTE))
  141.       {                            /* non-link command found */
  142.         i=i+2;
  143.         if((buffer[i]=='b')|(buffer[i]=='i')|(buffer[i]=='u')|(buffer[i]=='B')|(buffer[i]=='I')|(buffer[i]=='U')) i++;
  144.         if((buffer[i]=='b')|(buffer[i]=='i')|(buffer[i]=='u')|(buffer[i]=='B')|(buffer[i]=='I')|(buffer[i]=='U')) i++;
  145.         if(buffer[i]==KET) continue;
  146.  
  147.         if(((buffer[i]=='f')&(buffer[i+1]=='g'))|((buffer[i]=='F')&(buffer[i+1]=='G'))) /* foreground color */
  148.         {                                       /* skip to end      */
  149.           for (j=i+2;j<strlen(buffer);j++)
  150.           {
  151.             if (buffer[j]==KET) { i=j; break; }
  152.           }
  153.         }
  154.       }
  155.       else strncat(buff2,&buffer[i],1);
  156.     }
  157.  
  158. /* all retained chars are now in buff2. Ready to copy back to buffer */
  159. /* now deal with links */
  160.  
  161.     buffer[0]='\0';
  162.  
  163.     for (i=0;i<strlen(buff2);i++)
  164.     {
  165.       if (buff2[i]=='\0') break;             /* end of line */
  166.       if ((buff2[i]==AT)&(buff2[i+1]==BRA)&(buff2[i+2]==QUOTE))
  167.       {                               /* link command found */
  168.         endlink=FALSE;
  169.         for (j=i+3;j<strlen(buff2);j++)
  170.         {
  171.           if (buff2[j]==QUOTE)
  172.           {
  173.             for (j=j+1;j<strlen(buff2);j++)
  174.             { 
  175.               if (buff2[j]==KET) 
  176.               { i=j; endlink=TRUE; break; } 
  177.             }
  178.           }
  179.           if (endlink)  /* break; */
  180.           {
  181.             i=j;
  182.             break;
  183.           }
  184.           strncat(buffer,&buff2[j],1);
  185.           i=j;
  186.         }
  187.       }
  188.       else strncat(buffer,&buff2[i],1);
  189.     }
  190.  
  191.     fprintf(Outhandle,"%s",buffer);
  192.     linecount++;
  193.     if ((formfeeds)&(linecount >= textlength))
  194.     {
  195.       fputc('\x0c',Outhandle);  /* formfeed */
  196.       linecount = 0;  /* Reset at new page */
  197.     }
  198.   }
  199.  
  200.   if(formfeeds) fputc('\x0c',Outhandle);
  201.   fclose(Outhandle);
  202.   fclose(Inhandle);
  203.  
  204.   printf("Done\n");
  205.   return(0);
  206. }
  207.  
  208. /*--------------------------------------------------*/
  209.